# -*- coding: utf-8 -*-
# @Author : HaleShi
# @Time : 2023-06-25
# @File : 10_java_hook_key_iv.py

import frida
import sys


str_host = "127.0.0.1:8877"
manager = frida.get_device_manager()
rdev = manager.add_remote_device(str_host)
pid = rdev.spawn(["com.achievo.vipshop"])
session = rdev.attach(pid)

scr = """
Java.perform(function () {
    var SecretKeySpec = Java.use("javax.crypto.spec.SecretKeySpec");
    var ByteString = Java.use("com.android.okhttp.okio.ByteString");
    SecretKeySpec.$init.overload('[B', 'java.lang.String').implementation = function (key, name) {
        if (name === 'AES') {
            console.log("-----------------------SecretKeySpec---------------------------");
            console.log("4.key bytes=", JSON.stringify(key));
            console.log("4.key hex =", ByteString.of(key).hex());
            console.log("4.key", ByteString.of(key).utf8());
        }
        var res = this.$init(key, name);
        return res;
    }
    
    var IvParameterSpec = Java.use("javax.crypto.spec.IvParameterSpec");
        var ByteString = Java.use("com.android.okhttp.okio.ByteString");
        IvParameterSpec.$init.overload('[B').implementation = function (iv) {
            console.log("-----------------------IvParameterSpec---------------------------");
            console.log("iv str=", ByteString.of(iv).utf8());
            console.log("iv hex=", ByteString.of(iv).hex());
            console.log("iv byte", JSON.stringify(iv));
            var res = this.$init(iv);
            return res;
        }
});
"""
script = session.create_script(scr)


def on_message(message, data):
    print(message, data)

script.on("message", on_message)

script.load()
rdev.resume(pid)
sys.stdin.read()